//
//  ZYTagViewController.swift
//  ZYUICommon_Example
//
//  Created by Yi Zhou on 2025/1/8.
//  Copyright © 2025 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import ZYUICommon

class ZYTagViewController: UIViewController {
    
    var dataSource: [String] = [
        "请放心",
        "您无需担心您在此文件中",
        "提供的信息的准确性",
        "如果出现任何差异",
        "提交",
        "您的申请以供审核后",
        "Apple",
        "通过电子邮件通知您",
        "您只需纠正错误",
        "重新提交您的申请以供审核",
    ]
    
    lazy var backButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 64, y: zScreenHeight - zSafeBottomMargin - 64, width: 44, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("返回")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.dismiss(animated: true)
            }
    }()
    
    lazy var tagView: ZYTagView = {
        let tagView = ZYTagView(frame: CGRect(x: 10, y: zTopHeight + 150, width: zScreenWidth - 20, height: 100))
        tagView.backgroundColor = .cyan
        tagView.tDelegate = self
        tagView.alignment = .left
        return tagView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(backButton)
        view.addSubview(tagView)
        tagView.reloadData()
    }
}

extension ZYTagViewController: ZYTagViewDelegate {
    func tagViewNumberOfItem() -> Int {
        dataSource.count
    }
    
    func tagView(cellForRowAt index: Int) -> UIView {
        let title = dataSource[index]
        return UILabel().zyFont(14)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyTextColor(.black)
            .zyAllCornerRadius(5)
            .zyText(title)
    }
    
    func tagView(sizeForRowAt index: Int) -> CGSize {
        let title = dataSource[index]
        let width = ZYUtil.stringWidth(title, UIFont.systemFont(ofSize: 14), 32)
        return CGSize(width: width, height: 32)
    }
    
    func tagView(didSelectRowAt index: Int) {
        let title = dataSource[index]
        debugPrint(title)
    }
    
}
